package com.linapex.manager;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.linapex.dao.TestData;
import com.linapex.models.User;
import com.yhm.pagination.PageUtil;

public class UserManager {
	
	private Logger logger=Logger.getLogger(getClass());
	
	public List<User> findAll(PageUtil pageUtil){
		logger.debug("分页查询用户");
		Integer toIndex=0;
		if(pageUtil!=null) {pageUtil.setTotalCount(TestData.list.size());
			toIndex = pageUtil.getOffset()+pageUtil.getPageSize();
			toIndex=toIndex < pageUtil.getTotalCount()? toIndex:pageUtil.getTotalCount();
		}
		return TestData.list.subList(pageUtil.getOffset(),toIndex);
	}
	public List<User> findAll(User user,PageUtil pageUtil){
		logger.debug("分页查询用户");
		List<User> lus=new ArrayList<User>();
		//查询满足条件的用户信息
		if(user==null) user=new User(); 
		logger.debug("传入名字:"+user.getName());
		for (User u : TestData.list) {
			if(user.getName()==null||"".equals(user.getName())){
				lus.add(u);
			}else {
			  if(u.getName().indexOf(user.getName())>=0)  lus.add(u);					
			}						
		}
		Integer toIndex=0;
		if(pageUtil!=null) {pageUtil.setTotalCount(lus.size());
			toIndex = pageUtil.getOffset()+pageUtil.getPageSize();
			toIndex=toIndex < pageUtil.getTotalCount()? toIndex:pageUtil.getTotalCount();
		}		
		return lus.subList(pageUtil.getOffset(),toIndex);
	}
}
